<?php
class Controller_Admin_User extends FLEA_Controller_Action
{
	private $_ModelSysRoles;
	private $_ModelSysUsers;
	private $siteInfo;

	function Controller_Admin_User(){
		$this->siteInfo = FLEA::getAppInf('siteInfo');
		$this->_ModelSysRoles = & FLEA::getSingleton('Model_SysRoles');
		$this->_ModelSysUsers = & FLEA::getSingleton('Model_SysUsers');
		$this->_ModelSysUsers -> encodeMethod = PWD_MD5;
	}

	function actionAdd() {
		$companyNature = FLEA::getAppInf('companyNature');
		$roles = $this->_ModelSysRoles->findAll();
		$viewData = array(
			 'rolesList' => $roles,
			 'Title' => '添加新用户',
			 'companyNature'=>$companyNature, 
			 'siteInfo' => $this -> siteInfo,
		);
		$this->_executeView('Admin_Users_Add.html',$viewData);

	}

	function actionAddSave(){
		foreach($_POST['dispatching_name'] as $keys=>$values){
			if(!$values || !$_POST['dispatching_area'][$keys])continue;
			$dispatching[$keys]['name'] = $values;
			$dispatching[$keys]['area'] = $_POST['dispatching_area'][$keys];
			if($_POST['dispatching_id'][$keys]){
				$dispatching[$keys]['dispatching_id'] = $_POST['dispatching_id'][$keys];
			}
		}
		$list = array(
				'username' => addslashes($_POST['username']),
				'password' => addslashes($_POST['password']),
				'email' => addslashes($_POST['email']),
				'registerIp'=>isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1',
				'roles' => $_POST['ids']?$_POST['ids']:array('2'),
				'declareperson' => array(
					'name' => addslashes($_POST['pname']),
					'cellphone' => addslashes($_POST['cellphone']),
		),
				'declarecompany' => array(
					'name' => addslashes($_POST['name']),
					'permitNumber' => addslashes($_POST['permitNumber']),
					'permitValideDate' => addslashes($_POST['permitValideDate']),
					'telephone' => addslashes($_POST['telephone']),
					'contactPerson' => addslashes($_POST['contactPerson']),
					'contactPersonCellphone' => addslashes($_POST['contactPersonCellphone']),
					'contactPersonTelephone' => addslashes($_POST['contactPersonTelephone']),
					'manageNumber' => addslashes($_POST['manageNumber']),
					'manageValideDate' => addslashes($_POST['manageValideDate']),
					'legalPerson' => addslashes($_POST['legalPerson']),
					'legalPersonCellphone' => addslashes($_POST['legalPersonCellphone']),
					'nature' => addslashes($_POST['nature']),
					'cTelephone' => addslashes($_POST['cTelephone']),
					'resPerson' => addslashes($_POST['resPerson']),
					'resPersonCellphone' => addslashes($_POST['resPersonCellphone']),
					'resPersonTelephone' => addslashes($_POST['resPersonTelephone']),
		),
				'dispatchingcompany' => $dispatching,
		);
		//dump($list);exit;
		$sysusers =& FLEA::getSingleton('Model_SysUsers');
		$sysusers -> encodeMethod = PWD_MD5;//set encode
		$sysusers -> functionFields = FLEA::getAppInf('functionFields');

		do{
			$user = $sysusers->findByUsername($_POST['username']);
			if ($user) {
				$msg = '用户名已经存在，请更换用户名';
				js_alert($msg,'',url('Admin_User','Add'));
			}
			$sysusers -> create($list);
			$msg = '用户添加成功';
			js_alert($msg,'',url('Admin_User','List'));
		}
		while(false);

		redirect(url('Admin_User','List'));
	}

	function actionCheckUser(){
		$user = $this->_ModelSysUsers->findByUsername($_POST['username']);
		if($user){
			echo 1;
		}else{
			echo 0;
		}
	}

	function actionList(){
		$page = isset ( $_GET ['page'] ) ? ( int ) $_GET ['page'] : 0;
		$pageSize = 20;
		$order = " user_id ";

		FLEA::loadHelper ( 'pager' );
		$pager =  new FLEA_Helper_Pager ( $this->_ModelSysUsers, $page, $pageSize,null, $order);
		$arranges = $pager->findAll ();
		//		dump($arranges);exit;
		foreach ($arranges as $key=>$arrang) {
			$tmp=array();
			foreach ($arrang['roles'] as $roles) {
				//				dump($roles);exit;
				$tmp[]=$roles['rolename'];
				//dump();
			}
			$arranges[$key]['rolesname']=implode(',',$tmp);
		}

		//		dump($arranges);exit;
		$viewdate = array (
		 'list' => $arranges,
		 'Title' => '用户信息管理', 
		 'page' => $page, 
		 'pagerData' =>$pager->getPagerData(),
		);
		//dump($viewdate);exit;
		$this->_executeView('Admin_User_List.html',$viewdate);
	}

	function actionDelete() {
		$page = isset ( $_POST ['page'] ) ? ( int ) $_POST ['page'] : 0;
		$ids = $_POST ['ids'];

		if(!empty($ids)){
			foreach ($ids as $id){
				$user = $this->_ModelSysUsers -> findByUserId($id);
				$this->_ModelSysUsers -> remove($user);
			}
		}
		js_alert("删除成功",'',$this->_url("List",array('page'=>$page)));
	}

	function actionModify() {
		$id = isset ( $_GET ['id'] ) ? ( int ) $_GET ['id'] : 0;
		//		dump($id);exit;
		$companyNature = FLEA::getAppInf('companyNature');
		$roles=$this->_ModelSysRoles->findAll();
		$rowse=$this->_ModelSysUsers->find($id);
		$tmp=array();
		foreach ($rowse['roles'] as $key=>$arrang) {
			$tmp[]=$arrang['role_id'];
		}
		//$rowse['rolesids']=$tmp;
		$viewData = array(
			 'rowse' => $rowse,
			 'rolesList' => $roles,
			 'Title' => '修改用户信息', 
			 'companyNature'=>$companyNature,
			 'dispatching'=>$rowse['dispatchingcompany'],
			 'company'=>$rowse['declarecompany'],
		);
		//		dump($viewData);exit;
		$this->_executeView('Admin_Users_Modify.html',$viewData);

	}

	function actionModifySave() {
		//		dump($_POST);exit;
		foreach($_POST['dispatching_name'] as $keys=>$values){
			if(!$values || !$_POST['dispatching_area'][$keys])continue;
			$dispatching[$keys]['name'] = $values;
			$dispatching[$keys]['area'] = $_POST['dispatching_area'][$keys];
			if($_POST['dispatching_id'][$keys]){
				$dispatching[$keys]['dispatching_id'] = $_POST['dispatching_id'][$keys];
			}
		}
		$list = array(
			'user_id'=>addslashes($_POST['user_id']),
			'username' => addslashes($_POST['username']),
			'email' => addslashes($_POST['email']),
			'roles' => $_POST['ids']?$_POST['ids']:array('2'),
			'declareperson' => array(
					'id'=>addslashes($_POST['declareperson_id']),
					'name' => addslashes($_POST['pname']),
					'cellphone' => addslashes($_POST['cellphone']),
		),
			'declarecompany' => array(
					'declare_id'=> addslashes($_POST['declarecompany_id']),
					'name' => addslashes($_POST['name']),
					'permitNumber' => addslashes($_POST['permitNumber']),
					'permitValideDate' => addslashes($_POST['permitValideDate']),
					'telephone' => addslashes($_POST['telephone']),
					'contactPerson' => addslashes($_POST['contactPerson']),
					'contactPersonCellphone' => addslashes($_POST['contactPersonCellphone']),
					'contactPersonTelephone' => addslashes($_POST['contactPersonTelephone']),
					'manageNumber' => addslashes($_POST['manageNumber']),
					'manageValideDate' => addslashes($_POST['manageValideDate']),
					'legalPerson' => addslashes($_POST['legalPerson']),
					'legalPersonCellphone' => addslashes($_POST['legalPersonCellphone']),
					'nature' => addslashes($_POST['nature']),
					'cTelephone' => addslashes($_POST['cTelephone']),
					'resPerson' => addslashes($_POST['resPerson']),
					'resPersonCellphone' => addslashes($_POST['resPersonCellphone']),
					'resPersonTelephone' => addslashes($_POST['resPersonTelephone']),
		),
				'dispatchingcompany' => $dispatching,
		);

		//		dump($list);exit;
		$this->_ModelSysUsers -> save($list);

		$newpassword = isset ( $_POST ['newpassword'] ) ? $_POST ['newpassword'] : '';
		$newpassword1 = isset ( $_POST ['newpassword1'] ) ? $_POST ['newpassword1'] : '';

		$rowse = $this->_ModelSysUsers->find($_POST['user_id']);
		if ($newpassword && $newpassword1) {//只有旧密码正确时，才能修改密码;否则只修改信息
			if ($newpassword == $newpassword1 ){
				if (!$this->_ModelSysUsers->updatePassword($rowse['username'], $newpassword)) {
					js_alert("修改密码未通过！",'',url('Admin_User', 'Modify&id='.$_POST['user_id']));
				}else {
					js_alert("修改密码成功！",'',url('Admin_User', 'List'));				
				}
			}else{
				js_alert("两次输入的密码不相同！",'',url('Admin_User', 'Modify&id='.$_POST['user_id']));
			}	
		}
		js_alert("信息修改成功！",'',url('Admin_User', 'List'));	
	}
}
?>